package cn.data.stream.config;

import cn.data.stream.annotation.DataStream;
import com.google.common.collect.Sets;
import java.util.Arrays;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;

/**
 * @author shi hong yu
 */
@Slf4j
public class DataStreamClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

    public DataStreamClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
        super(registry,useDefaultFilters);
    }

    public void registerFilters() {
        addIncludeFilter(new AnnotationTypeFilter(DataStream.class));
    }

    public Set<BeanDefinitionHolder> doScan(String... namespaces) {
        if(namespaces==null||namespaces.length==0){
            namespaces =new String[] {DataStreamConsts.NAMESPACE_APPLICATION};
        }else{
            Set<String> namespaceApplication = Sets.newHashSet(namespaces);
            namespaceApplication.add(DataStreamConsts.NAMESPACE_APPLICATION);
            namespaces=
                namespaceApplication.stream().toArray(String[]::new);
        }
        log.info("扫描dataStream 包:{}", Arrays.toString(namespaces));
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(namespaces);
        for(BeanDefinitionHolder h: beanDefinitionHolders){
            log.debug("扫描到bean:{} {}",h.getBeanName(),h.getBeanDefinition().getBeanClassName());
        }
        return beanDefinitionHolders;
    }
}
